package com.jackey.sms.controller;

import cn.hutool.core.util.RandomUtil;
import com.jackey.sms.service.UserService;
import com.jackey.sms.utils.RedisUtil;
import com.jackey.sms.utils.Result;
import com.jackey.sms.utils.ResultCode;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 小伟
 * @version V1.0
 * @Package com.jackey.sms.controller
 * @date 2020/12/18 9:34
 * @Copyright © 2020-2021 郑州恒星科技有限责任公司
 */
@RestController
public class UserController {
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    UserService userService;

    @GetMapping("/sendsms")
    public Result sendMessage(String mobile) {
        //生成随机验证码
        String code = RandomUtil.randomString("0123456789", 6);
        //发送短信
//        SendSmsUtil.sendsms(mobile, code);
        //保存验证码到redis
        redisUtil.set(mobile, code, 60);
        return new Result(ResultCode.SEND_SUCCESS);
    }

    @PostMapping("/login")
    public Result login(@RequestBody Map<String, String> loginMap) {
        //获取用户手机号
        String mobile = loginMap.get("mobile");
        //验证验证码
        try {
            Object code_ = redisUtil.get(mobile);
            //验证验证码
            String code = loginMap.get("code");
            if (!code.equals(code_)) {
                throw new Exception();
            }
        } catch (Exception e) {
            return new Result(ResultCode.CODE_ERROR);
        }
        //登录
        String password = loginMap.get("password");
        UsernamePasswordToken upToken = new UsernamePasswordToken(mobile, password);
        Subject subject = SecurityUtils.getSubject();
        //验证账户密码
        try {
            subject.login(upToken);
            String session = (String) subject.getSession().getId();
            return new Result(ResultCode.SUCCESS,session);
        } catch (Exception e) {
            return new Result(ResultCode.MOBILEORPASSOWRD_ERROR);
        }
    }

//    @RequiresRoles("系统管理员")
    @PostMapping("/user/home")
    public Result home() {
        return new Result(10000,"访问首页成功",true) ;
    }

    @RequiresPermissions("user-add")
    @PostMapping("/user")
    public Result addUser() {
        return new Result(10000,"添加用户成功",true) ;
    }

    @RequiresPermissions("user-update")
    @PutMapping("/user")
    public Result updateUser() {
        return new Result(10000,"修改用户成功",true) ;
    }

    @RequiresPermissions("user-delete")
    @DeleteMapping("/user")
    public Result delUser() {
        return new Result(10000,"删除用户成功",true) ;
    }

    @RequiresPermissions("user-find")
    @GetMapping("/user")
    public Result findUser() {
        return new Result(10000,"查询用户成功",true) ;
    }

    @GetMapping("/autherror")
    public String autherror(int code) {
        return code == 1 ? "未登录" : "未授权";
    }
}
